package com.jbh.recordlibrary.v;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.jbh.recordlibrary.R;
import com.jbh.recordlibrary.u.ScreenUtil;
import com.jbh.recordlibrary.u.Utils;

public class RecordTimeView extends LinearLayout {
    /**
     * 计算每一秒的宽度
     */
    private float itemWidth = 100;
    private int itemHeight ;
    private int color = Color.GRAY;

    public RecordTimeView(Context context) {
        this(context,null);
    }

    public RecordTimeView(Context context,  AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RecordTimeView(Context context,  AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        removeAllViews();
        setOrientation(HORIZONTAL);
        itemWidth = ScreenUtil.dpToPx(getContext(),50);
        itemHeight = ScreenUtil.dpToPx(getContext(),20);
    }


    public void setViewColor(int color){
        this.color=color;
    }


    /**
     * 绘制的总时间
     * @param totalTime
     */
    public void setData(float itemWidth , float totalTime){
        this.itemWidth=itemWidth;
        removeAllViews();
        for (int i=1;i<=Math.ceil(totalTime*1.0/1000);i++){
            addChildView(i);
        }
    }



    private void addChildView(int i) {
        View view=LayoutInflater.from(getContext()).inflate(R.layout.item_time_layout,null);
        LinearLayout.LayoutParams layoutParams= new LinearLayout.LayoutParams((int) itemWidth,itemHeight);
        addView(view,layoutParams);
        childView(view,i);
    }


    private void childView(View view, int i) {
        if (i==1){
            view.findViewById(R.id.line0).setVisibility(VISIBLE);
        }

        TextView tv_time = view.findViewById(R.id.tv_time);
        tv_time.setText(Utils.formatSecond1(i*1000));

        tv_time.setTextColor(color);

        View line =view.findViewById(R.id.line);
        line.setBackgroundColor(color);
    }
}
